<div id="panel-button" class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
				@if(isset($product->id))
				{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $product->id))}}
				@endif
				{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
			</div>
		</div>
	</div>
</div>

<div id="panel-button-fixed" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
				@if(isset($product->id))
				{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $product->id))}}
				@endif
				{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
			</div>
		</div>
	</div>
</div>

<div id="panel-error" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div id="error-content" class="panel-body"></div>
		</div>
	</div>
</div>

<div class='row'>
	<div class='col-md-12'>
		<div class="panel panel-default">
			<!--<header class="panel-heading">{{ trans('product.create') }}</header>-->
			<div class="panel-body">
				{{Form::open(array('method' => 'post', 'class' => 'form-horizontal'))}}
					<div class="form-group">
						{{Form::label('name', trans('product.name'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('name', (isset($product->name))? $product->name : null, array('class' => 'form-control', 'id' => 'name', 'placeholder' => trans('product.name')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('net', trans('product.net'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('net', (isset($product->net))? $product->net : null, array('class' => 'form-control', 'id' => 'net', 'placeholder' => trans('product.net')))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('brand', trans('product.brand'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::select('brand', $brands, (isset($product->brand))? $product->brand : null, array('class' => 'form-control'))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('type', trans('product.type'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::select('type', $types, (isset($product->type))? $product->type : null, array('class' => 'form-control'))}}
						</div>
					</div>

					<div class="form-group">
						{{Form::label('price', trans('product.price'), array('class' => 'col-sm-2 control-label'))}}
						<div class="col-sm-5">
							{{Form::text('price', (isset($product->price))? $product->price : null, array('class' => 'form-control', 'id' => 'price', 'placeholder' => trans('product.price')))}}
						</div>
					</div>

					{{Form::hidden('id', (isset($product->id))? $product->id : 0, array('id' => 'id'))}}
				{{Form::close()}}
			</div>
		</div>
	</div>
</div>

<script>
$(document).ready(function(){
	$("#submit").click(function(){
		var name = $("#name").val();
		var net = $("#net").val();
		var brand = $("#brand").val();
		var type = $("#type").val();
		var price = $("#price").val();
		var id = $("#id").val();
		var data = {'name': name, 'net': net, 'brand': brand, 'type': type, 'price': price, 'quantity': 0};

		if(id > 0){
			data = {'name': name, 'net': net, 'brand': brand, 'type': type, 'price': price, 'quantity': 0, 'id': id};
		}

		deleteErrorMessages();

		$.ajax({
			url: "{{URL::to('/admin/product/create')}}",
			type: "POST",
			beforeSend: ajaxLoading(),
			data: data,
			success: function(result){
				var rs = $.parseJSON(result);
				
				if(rs.error == true){
					appendErrorMessages(rs.message);
				}else{
					window.location = "{{URL::to('/admin/product')}}";
				}

				ajaxLoadSuccess();
			}
		});
	});

	$("button#delete").click(function(){
		var ok = confirm("{{trans('product.confirm_delete_this')}}");

		if(ok){
			var id = $(this).attr('data-id');

			$.ajax({
				url: '{{URL::to("admin/product/delete")}}',
				type: 'POST',
				data: {'id': id},
				success: function(result){
					var url = "{{URL::to('/admin/product/')}}";

					window.location = url;
				}
			});
		}
	});

	$("button#cancel").click(function(){
		var url = "{{URL::to('/admin/product/')}}";

		window.location = url;
	});
});
</script>